// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <glob.h>

constexpr float kCompressionRatioInitialGuess = 0.3f;
constexpr float kFlushOverheadRatioInitialGuess = 0.1f;
// TODO(T79660637): make write cost estimate more granular.
constexpr float kAverageRowSizeInitialGuess = 0.0f;

namespace kumo::pollux::dwrf {

class RatioTracker {
 public:
  explicit RatioTracker(float initialGuess);
  virtual ~RatioTracker() = default;

  float getEstimatedRatio() const;
  virtual bool isEmptyInput(size_t denominator, size_t numerator);
  void takeSample(size_t denominator, size_t numerator);
  size_t getSampleSize() const;

 private:
  float workingAverage_;
  size_t sampleSize_{0};
  size_t numeratorTotal_{0};
  size_t denominatorTotal_{0};
};

class CompressionRatioTracker final : public RatioTracker {
 public:
  explicit CompressionRatioTracker(
      float initialGuess = kCompressionRatioInitialGuess);

  bool isEmptyInput(size_t denominator, size_t numerator) override;
};

class FlushOverheadRatioTracker final : public RatioTracker {
 public:
  explicit FlushOverheadRatioTracker(
      float initialGuess = kFlushOverheadRatioInitialGuess);
};

class AverageRowSizeTracker final : public RatioTracker {
 public:
  explicit AverageRowSizeTracker(
      float initialGuess = kAverageRowSizeInitialGuess);
};

} // namespace kumo::pollux::dwrf
